package frsf.cidisi.exercise.modelo.util;
import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import frsf.cidisi.exercise.modelo.situationCalculus.AmbienteEstado;

public class JLaberinto extends JPanel {
	private static final long serialVersionUID = 1L;
 
    public JLaberinto(AmbienteEstado ambienteEstado, int nivel) {
        super(new GridLayout(1,1));
 
        this.setLayout(new BorderLayout());
        
        Celda[][] celdas = ambienteEstado.getLaberinto().getCeldas(); 
       
        JPanel filas = new JPanel();
        filas.setLayout(new BoxLayout(filas, BoxLayout.X_AXIS));
        
        for(Coordenada posicion : ambienteEstado.getListaEntradas()) {
        	Celda celda = ambienteEstado.getLaberinto().getCelda(posicion);
        	celda.setEntrada(true);
        }
        
        for(Coordenada posicion : ambienteEstado.getListaLlaves()) {
        	Celda celda = ambienteEstado.getLaberinto().getCelda(posicion);
        	celda.setLlave(true);
        }
        
        for(Salida salida : ambienteEstado.getListaSalidas()) {
        	Coordenada coordenada = salida.getCoordenada();
        	Celda celda = ambienteEstado.getLaberinto().getCelda(coordenada);
        	celda.setSalida(true);
        	celda.setCerrada(salida.getCerrada());
        }
        
        for(int i = 0; i < celdas.length; i ++) {
        	JPanel fila = new JPanel();
            fila.setLayout(new BoxLayout(fila, BoxLayout.Y_AXIS));
        	for(int j = 0; j < celdas[i].length; j ++) {
        		if(celdas[j][i] != null) {
        			fila.add(new JPanelCelda(celdas[j][i]));
        		}
            }
        	filas.add(fila);
        }
        
        
        //Create the scroll pane and add the table to it.
        JScrollPane scrollPane = new JScrollPane(filas);

        //Add the scroll pane to this panel.
        add(scrollPane, BorderLayout.CENTER);
        
      //Create and set up the window.
        JFrame frame = new JFrame("Laberinto " + nivel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        
        //Create and set up the content pane.
        this.setOpaque(true); //content panes must be opaque
        frame.setContentPane(this);
//        frame.setMinimumSize(new Dimension(600, 600));
//        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
//        Point middle = new Point(screenSize.width / 2, screenSize.height / 2);
//        Point newLocation = new Point(middle.x - (frame.getWidth() / 2), 
//                                      middle.y - (frame.getHeight() / 2));
//        frame.setLocation(newLocation);
        
        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }
}